//
//  MeSetUserName.m
//  ZITHER
//
//  Created by weixin on 2018/11/15.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "MeSetUserName.h"
#import "UgcCommentKewboardV.h"


@interface MeSetUserName ()<QMUITextFieldDelegate>

//@property (nonatomic,strong)UIfieldLogin *fieldLogin;
@property (nonatomic,strong)QMUITextField *fieldLogin;

@property (nonatomic,strong)BaseLab *redTipe;//报错提示

@end

@implementation MeSetUserName

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    [self setUI];
        
}

-(void)setNav{
    [self.baseNavigationView setHiddenNav:NO VC:self];

    /* TODO: ================= 导航栏V4样式 =================*/
    [self.baseNavigationView NavigationBarStyle:1];
    self.baseNavigationView.leftButton1.hidden = YES;
    self.baseNavigationView.navViewStyle.title.text = @"修改用户名";
}

-(void)setUI{
    
    [self.view addSubview:self.fieldLogin];
    
    [self.fieldLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20+NaviHeight);
        make.left.mas_equalTo(10);
        make.width.mas_equalTo(kScreenW-20);
        make.height.mas_equalTo(40);
    }];
    
//    [self.view addSubview:self.fieldLogin];
//    [self.fieldLogin mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(20+NaviHeight);
//        make.left.mas_equalTo(10);
//        make.width.mas_equalTo(kScreenW-20);
//        make.height.mas_equalTo(40);
//    }];

    [self.view addSubview:self.redTipe];
    [self.redTipe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.fieldLogin.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(15);
    }];
    
    BaseBtn *btn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, 0, 1, 1) NormalText:@"确定" NormalTextColor:baseColorTheme SeleText:@"确定" SeleTextColor:baseColorTheme font:15 isBold:NO];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setCornerRadiusWithRadius:4 isMasks:YES];
    [btn setBorderWithColor:baseColorTheme width:1];
    [self.view addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(self.redTipe.mas_bottom).offset(15);
        make.height.mas_equalTo(44);
    }];
    
    @weakify(self);
    [[btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self revampedUserName];
    }];
    [self.fieldLogin becomeFirstResponder];
}


-(void)revampedUserName{
    
    NSString *name = self.fieldLogin.text;
    
    if (name.length > 0&&name.length <11) {
        
        /* TODO: ================= 修改名称 =================*/
        
        @weakify(self);
        [[AccountService loadsaveUserInfo:name]subscribeNext:^(ResponseData  *x) {
            @strongify(self);
            if (x.success) {
                kUSER_INFO_MANAGER.nickName = name;
                [self.navigationController popViewControllerAnimated:YES];
                [Tools showTips:@"修改用户名称成功"];
//                self.redTipe.hidden = NO;
//                self.redTipe.text = @"修改用户名称成功";
            }else{
                self.redTipe.hidden = NO;
                self.redTipe.text = x.errorInfo;
//                [QMUITips showError:x.errorInfo];
            }
            
        }];
        
    } else {
        self.redTipe.hidden = NO;
        self.redTipe.text = @"不能为空或者超过10个长度";
//        [Tools showTips:@"不能为空或者超过10个长度"];
    }
}


-(QMUITextField *)fieldLogin{
    if (!_fieldLogin) {
        _fieldLogin = [[QMUITextField alloc]initWithFrame:CGRectMake(0, 100, 150, 50)];
        _fieldLogin.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17];

        _fieldLogin.delegate = self;
        _fieldLogin.placeholderColor = [UIColor whiteColor]; // 自定义 placeholder 的颜色
        _fieldLogin.returnKeyType = UIReturnKeyDone;
        _fieldLogin.enablesReturnKeyAutomatically = YES;
        _fieldLogin.tintColor = [UIColor colorWithHex:@"333333"];
        // 限制可输入的字符长度
        _fieldLogin.maximumTextLength = 8;
        _fieldLogin.qmui_borderWidth = 1;
        _fieldLogin.qmui_borderPosition = QMUIViewBorderPositionBottom;
        //        _fieldLogin.keyboardType = UIKeyboardTypeNumberPad;
        _fieldLogin.qmui_borderColor = [UIColor colorWithHex:@"eeeeee"];
        
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        NSAttributedString *attri = [[NSAttributedString alloc] initWithString:@"输入用户名" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:@"999999"],NSFontAttributeName:[UIFont fontWithName:@"PingFang-SC-Regular" size:15], NSParagraphStyleAttributeName:style}];
        _fieldLogin.attributedPlaceholder = attri;
        
        
    }
    return _fieldLogin;
}
//- (void)fieldLoginDidChange:(UIfieldLogin *)fieldLogin
//{
//    if (fieldLogin.text.length > 10)
//    {
//        fieldLogin.text = [fieldLogin.text substringToIndex:10];
//    }
//    self.redTipe.hidden = YES;
//
//    //记录输入的字数，你若不统计字数，忽略这行
////    self.limitWordLable.text = [NSString stringWithFormat:@"%lu/%d", (unsigned long)[fieldLogin.text length], 200];
//}

//- (BOOL)fieldLogin:(UIfieldLogin *)fieldLogin shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//{
//    /** 限制字数 10个 */
//    NSString *str = [NSString stringWithFormat:@"%@%@", fieldLogin.text, text];
//
//    NSInteger textNum = 30;
//    if (str.length > textNum)
//    {
//        NSRange rangeIndex = [str rangeOfComposedCharacterSequenceAtIndex:textNum];
//
//        if (rangeIndex.length == 1)//字数超限
//        {
//            fieldLogin.text = [str substringToIndex:textNum];
//        }else{
//            NSRange rangeRange = [str rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, textNum)];
//            fieldLogin.text = [str substringWithRange:rangeRange];
//        }
//        return NO;
//    }
//    return YES;
//}

-(BaseLab *)redTipe{
    if (!_redTipe) {
        _redTipe = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"输入手机号错误" font:12 textColor:[UIColor colorWithHex:@"FF6F6F"] isBold:NO numberOfLines:0];
        _redTipe.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
        _redTipe.hidden = YES;
    }
    return _redTipe;
}


@end
